home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Magazine / C_Tutorial / Part-3 / gadgets3.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  9KB  |  340 lines

  1. #include<exec/libraries.h>
  2. #include<intuition/intuition.h>
  3. #include<utility/tagitem.h>
  4. #include<graphics/text.h>
  5. #include<graphics/rastport.h>
  6. #include<intuition/screens.h>
  7. #include<libraries/gadtools.h>
  8.  
  9. #include<string.h>
  10. #include<stdio.h>
  11.  
  12. #include<clib/exec_protos.h>
  13. #include<clib/graphics_protos.h>
  14. #include<clib/intuition_protos.h>
  15. #include<clib/layers_protos.h>
  16. #include<clib/gadtools_protos.h>
  17.  
  18. /* The library base global variables */
  19. /* (The different style of opening libraries requires these to be initialised to NULL) */
  20. struct Library* GfxBase = NULL;
  21. struct Library* IntuitionBase = NULL;
  22. struct Library* LayersBase = NULL;
  23. struct Library* GadToolsBase = NULL;
  24.  
  25. /* The global handle on the palette gadget */
  26. struct Gadget* palgad = NULL;
  27.  
  28. /* Need to give prototypes for our functions */
  29. void handleIDCMP(struct Window*);
  30. int setClip(struct Window*);
  31. void removeClip(struct Window*);
  32. void setupWindow();
  33. void createWindow(struct Gadget*);
  34. int openLibs();
  35. void closeLibs();
  36.  
  37. /* Some constants for the size of the window */
  38. #define MYWIN_WIDTH        (400)
  39. #define MYWIN_HEIGHT    (200)
  40.  
  41. /* Some constants for the position and size of our gadget */
  42. #define MYBUT_LEFT        (10)
  43. #define MYBUT_TOP            (5)
  44. #define MYBUT_WIDTH        (80)
  45. #define MYBUT_HEIGHT    (12)
  46. #define MYBUT_TEXT        "Next Pen"
  47. #define MYBUT_ID            (0)
  48.  
  49. #define MYPAL_LEFT        (170)
  50. #define MYPAL_TOP            (2)
  51. #define MYPAL_WIDTH        (109)
  52. #define MYPAL_HEIGHT    (19)
  53. #define MYPAL_TEXT        "Colour:"
  54. #define MYPAL_ID            (1)
  55. #define MYPAL_DEPTH        (4)
  56.  
  57. /* The top gap required around the gadgets */
  58. #define MYTOPGAP      (30)
  59.  
  60. /* The initial pen colour */
  61. #define MYINITPEN            (1)
  62.  
  63. /* The start of the program */
  64. void main()
  65. {
  66.     /* Use a different style of opening libraries... */
  67.     if(openLibs())
  68.     {
  69.         /* Now do the real work */
  70.         setupWindow();
  71.     }
  72.     /* Matched call to close libraries */
  73.     closeLibs();
  74. }
  75.  
  76. /* Try to open all the libraries -- return TRUE on success */
  77. int openLibs()
  78. {
  79.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  80.     {
  81.         printf("Error: could not open graphics.library\n");
  82.         return FALSE;
  83.     }
  84.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  85.     {
  86.         printf("Error: could not open intuition.library\n");
  87.         return FALSE;
  88.     }
  89.     if((LayersBase = OpenLibrary("layers.library",37)) == NULL)
  90.     {
  91.         printf("Error: could not open layers.library\n");
  92.         return FALSE;
  93.     }
  94.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  95.     {
  96.         printf("Error: could not open gadtools.library\n");
  97.         return FALSE;
  98.     }
  99.   return TRUE;
  100. }
  101.  
  102. /* Close any open library */
  103. void closeLibs()
  104. {
  105.     if(GadToolsBase)
  106.         CloseLibrary(GadToolsBase);
  107.     if(LayersBase)
  108.         CloseLibrary(LayersBase);
  109.     if(IntuitionBase)
  110.         CloseLibrary(IntuitionBase);
  111.     if(GfxBase)
  112.         CloseLibrary(GfxBase);
  113. }
  114.  
  115. /* Setup the window -- do the GadTools stuff */
  116. void setupWindow()
  117. {
  118.     struct Screen* scr;
  119.     /* We'll copy the visual information for the default public screen */
  120.   /* (usually, this is the Workbench screen) */
  121.     if(scr = LockPubScreen(NULL))
  122.     {
  123.         APTR vinfo;
  124.         /* Get the visual info so GadTools can render the gadgets nicely */
  125.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  126.         {
  127.             /* We can initialise glist in its declaration */
  128.             struct Gadget* glist = NULL;
  129.             struct Gadget* gad;
  130.             int offtop, offleft;
  131.             struct NewGadget newgad;
  132.             /* Initialised structure declaration: describes 8pt Topaz font */
  133.             struct TextAttr topazFont = { "topaz.font", 8, 0, 0, };
  134.             /* Start a GadTools gadget list */
  135.             gad = CreateContext(&glist);
  136.             /* The offsets of our window borders */
  137.             offleft = scr->WBorLeft;
  138.             offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  139.  
  140.             /* Setup our first gadget */
  141.             newgad.ng_TextAttr         = &topazFont;
  142.             newgad.ng_VisualInfo     = vinfo;
  143.             newgad.ng_LeftEdge         = MYBUT_LEFT + offleft;
  144.             newgad.ng_TopEdge         = MYBUT_TOP + offtop;
  145.             newgad.ng_Width             = MYBUT_WIDTH;
  146.             newgad.ng_Height             = MYBUT_HEIGHT;
  147.             newgad.ng_GadgetText    = MYBUT_TEXT;
  148.             newgad.ng_GadgetID        = MYBUT_ID;
  149.             newgad.ng_Flags                = 0;
  150.             /* Now create it and add it to our list */
  151.             gad = CreateGadget(BUTTON_KIND, gad, &newgad, TAG_END);
  152.  
  153.             /* Setup our second gadget */
  154.             /* (We can reuse newgad, and just change the different bits) */
  155.             newgad.ng_LeftEdge         = MYPAL_LEFT + offleft;
  156.             newgad.ng_TopEdge         = MYPAL_TOP + offtop;
  157.             newgad.ng_Width             = MYPAL_WIDTH;
  158.             newgad.ng_Height             = MYPAL_HEIGHT;
  159.             newgad.ng_GadgetText    = MYPAL_TEXT;
  160.             newgad.ng_GadgetID        = MYPAL_ID;
  161.             newgad.ng_Flags                = 0;
  162.             /* Now create it and add it to our list */
  163.             if(gad = CreateGadget(PALETTE_KIND, gad, &newgad,
  164.                                                         /* Initially selected pen */
  165.                                                         GTPA_Color, MYINITPEN,
  166.                                                         /* Depth: 2 to the power MYPAL_DEPTH colours */
  167.                                                         GTPA_Depth, MYPAL_DEPTH,
  168.                                                         /* Gadget will indicate selection */
  169.                                                         GTPA_IndicatorWidth, 16,
  170.                                                         TAG_DONE))
  171.             {
  172.                 /* Remember gadget pointer so we can affect it in message handler */
  173.                 palgad = gad;
  174.                 /* If succeeded then all gadgets created */
  175.                 createWindow(glist);
  176.             }
  177.             else
  178.                 printf("Error: could not create gadget(s)\n");
  179.             /* Free all the gadgets that were created */
  180.             FreeGadgets(glist);
  181.             FreeVisualInfo(vinfo);
  182.         }
  183.         else
  184.             printf("Error: could not get visual info\n");
  185.         UnlockPubScreen(NULL, scr);
  186.     }
  187.     else
  188.         printf("Error: could not lock public screen\n");
  189. }
  190.  
  191. /* Actually open the window, in the normal way */
  192. void createWindow(struct Gadget* glist)
  193. {
  194.     struct Window* win;
  195.     /* Open our window */
  196.     if(win = OpenWindowTags(NULL,
  197.                                                     WA_Width,        MYWIN_WIDTH,
  198.                                                     WA_Height,    MYWIN_HEIGHT,
  199.                                                     WA_Flags,        WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  200.                                                     WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | BUTTONIDCMP | IDCMP_REFRESHWINDOW,
  201.                                                     WA_Gadgets,    glist,
  202.                                                     TAG_DONE,        0))
  203.     {
  204.         /* If window opened, set clip region */
  205.         if(setClip(win))
  206.         {
  207.             /* Let GadTools refresh its bits of the window */
  208.             GT_RefreshWindow(win, NULL);
  209.             /* Now handle messages */
  210.             handleIDCMP(win);
  211.             removeClip(win);
  212.         }
  213.         else
  214.             printf("Error: could not set clip region on window\n");
  215.         CloseWindow(win);
  216.     }
  217.     else
  218.         printf("Error: could not open window\n");
  219. }
  220.  
  221. /* Our message handling code */
  222. void handleIDCMP(struct Window* win)
  223. {
  224.     char* text = "Hello World!";
  225.     int going = TRUE;
  226.     int drawing = FALSE;
  227.     UBYTE pen = MYINITPEN;
  228.     SetAPen(win->RPort, pen);
  229.     /* Set the drawing mode to draw only the foreground of text, not the background */
  230.     SetDrMd(win->RPort, JAM1);
  231.     while(going)
  232.     {
  233.         struct IntuiMessage* intuimsg;
  234.         /* Wait for messages to arrive */
  235.         WaitPort(win->UserPort);
  236.         /* Messages have arrived: loop through all of them */
  237.         while(intuimsg = GT_GetIMsg(win->UserPort))
  238.         {
  239.             /* Act on this message... */
  240.             switch(intuimsg->Class)
  241.             {
  242.             case IDCMP_MOUSEBUTTONS:
  243.                 switch(intuimsg->Code)
  244.                 {
  245.                 case SELECTDOWN:
  246.                     drawing = TRUE;
  247.                     break;
  248.                 case SELECTUP:
  249.                     drawing = FALSE;
  250.                     break;
  251.                 }
  252.                 /* break; omitted so we draw on click, too */
  253.             case IDCMP_MOUSEMOVE:
  254.                 /* Don't draw on top gap which holds gadgets */
  255.                 if(drawing && intuimsg->MouseY > win->BorderTop+MYTOPGAP)
  256.                 {
  257.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  258.                     Text(win->RPort, text, strlen(text));
  259.                 }
  260.                 break;
  261.             case IDCMP_CLOSEWINDOW:
  262.                 going = FALSE;
  263.                 break;
  264.             case IDCMP_REFRESHWINDOW:
  265.                 /* You *MUST* remember to ask for and handle these refresh messages */
  266.                 GT_BeginRefresh(win);
  267.                 GT_EndRefresh(win, TRUE);
  268.                 break;
  269.             case IDCMP_GADGETUP:
  270.                 /* Trick: introduce new "{..}" scope so we can declare gad locally */
  271.                 {
  272.                     struct Gadget* gad = (struct Gadget*)(intuimsg->IAddress);
  273.                     switch(gad->GadgetID)
  274.                     {
  275.                     case MYBUT_ID:
  276.                     {
  277.                         /* Our button was clicked!  Set foreground to next pen colour */
  278.                         /* (Wrap when reached the end of the palette gadget's colours) */
  279.                         pen = (pen+1) % (1<<MYPAL_DEPTH);
  280.                         SetAPen(win->RPort, pen);
  281.                         /* Update palette gadget with new pen value */
  282.                         GT_SetGadgetAttrs(palgad, win, NULL, GTPA_Color, pen, TAG_DONE);
  283.                         break;
  284.                     }
  285.                     case MYPAL_ID:
  286.                         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  287.                         pen = intuimsg->Code;
  288.                         SetAPen(win->RPort, pen);
  289.                         break;
  290.                     }
  291.                 }
  292.             }
  293.             /* Reply when finished with message */
  294.             GT_ReplyIMsg(intuimsg);
  295.         }
  296.     }
  297. }
  298.  
  299. /* Set a clip region on internal part of window */
  300. int setClip(struct Window* win)
  301. {
  302.     /* Make a new region */
  303.     struct Region* reg;
  304.     if(reg = NewRegion())
  305.     {
  306.         /* Make a rectangle that describes the inside of the window */
  307.         struct Rectangle rect;
  308.         rect.MinX = win->BorderLeft;
  309.         rect.MinY = win->BorderTop;
  310.         rect.MaxX = win->Width - win->BorderRight - 1;
  311.         rect.MaxY = win->Height - win->BorderBottom - 1;
  312.         /* Make the region equal to this rectangle */
  313.         if(OrRectRegion(reg, &rect))
  314.         {
  315.             /* Set the clip region on the window's layer */
  316.             InstallClipRegion(win->WLayer, reg);
  317.             /* Say we succeeded */
  318.             return TRUE;
  319.         }
  320.         else
  321.         {
  322.             /* Failed to set region, so delete it */
  323.             DisposeRegion(reg);
  324.         }
  325.     }
  326.     /* If we get this far they we've failed */
  327.     return FALSE;
  328. }
  329.  
  330. /* Remove the clip region from a window */
  331. void removeClip(struct Window* win)
  332. {
  333.     struct Region* reg;
  334.     if(reg = InstallClipRegion(win->WLayer, NULL))
  335.     {
  336.         /* If a clip region is installed it's our new one, so delete it */
  337.         DisposeRegion(reg);
  338.     }
  339. }
  340.